package wordsDistribution;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class WordsDistributionLoader {

	private Map<String, Long> global;
	
	private Map<String, Long> globalOccurrence; //the number of documents where a word appears.
	
	private Map<Integer,Map<String,Long>> local;
	
	public void load(String distribution) throws IOException {
		
		//HardCoded
		
		File f = new File(distribution);
		
		File[] files = f.listFiles();
		
		global = new HashMap<String, Long>();
		
		globalOccurrence = new HashMap<String, Long>();
		
		local = new HashMap<Integer,Map<String, Long>>();
		
		for (int i = 0; i < files.length; i++) {
			
			Map<String,Long> loc = new HashMap<String, Long>();
			
			if (files[i].isFile()){
			
				local.put(Integer.valueOf(files[i].getName()), loc);
				
				loadFile(files[i],loc,global,globalOccurrence);

			}
			
		}
		
		
		
	}

	private void loadFile(File file,
			Map<String, Long> local, Map<String, Long> global, Map<String, Long> globalOccurrence) throws IOException {
		
		BufferedReader br = new BufferedReader(new FileReader(file));
		
		String line;
		
		while ((line = br.readLine())!=null){
			
			String[] spl = line.split(WordsDistribution.SEPARATOR);
			
			String word = spl[0];
			
			if (!valid(word)){
				continue;
			}
			
			Long freq = Long.valueOf(spl[1]);
			
			local.put(word, freq);
			
			updateMap(global,word,freq);
			
			updateMap(globalOccurrence,word,1L);
			
		}
		
		br.close();
	}

	private boolean valid(String word) {
		
		if (word.length() <= 1)
			return false;
		
		char[] c = word.toCharArray();
		
		for (int i = 0; i < c.length; i++) {
			if (!Character.isLetter(c[i]))
				return false;
		}
		
		return true;
	}

	private void updateMap(Map<String, Long> map, String word,
			Long newfreq) {
		Long freq = map.get(word);
		if(freq==null){
			freq = 0L;
		}
		map.put(word, freq+newfreq);
	}
	
	public Set<Integer> getFiles(){
		return local.keySet();
	}
	
	public Map<String,Long> getWordMap(Integer index){
		return local.get(index);
	}
	
	public Long getGlobalValue(String word){
		return global.get(word);
	}
	
	public Set<String> getWords(){
		return global.keySet();
	}
	
	public Long getDocumentFrequency(String word){
		return globalOccurrence.get(word);
	}
}
